package com.example.zbh.lessona.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.zbh.lessona.R;

/**
 * Created by zbh on 15/11/26.
 */
public class FaceAdapter extends RecyclerView.Adapter<FaceAdapter.FaceViewHolder> {
    private int[] faces;

    public void addData(int[] faces) {
        this.faces = faces;

    }

    @Override
    public FaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_face, null);
        return new FaceViewHolder(view);
    }


    @Override
    public void onBindViewHolder(FaceViewHolder holder, int position) {
        if (faces != null && faces.length > 0) {
            holder.imageView.setBackgroundResource(faces[position]);
            holder.position = position;
        }


    }

    public boolean isLength() {
        return faces != null && faces.length > 0;
    }


    @Override
    public int getItemCount() {
        return isLength() ? faces.length : 0;
    }


    class FaceViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imageView;
        private int position;
        public FaceViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            imageView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.imageView:
                    if (listener != null){
                        listener.OnitemClick(position);
                    }

                    break;
            }
        }
    }

    public interface OnitemClickListener {
        void OnitemClick(int position);
    }
    private OnitemClickListener listener;
    public void setOnitemClickListener(OnitemClickListener listener){
        this.listener = listener;

    }
}
